package com.zhongketech.sunflower.filter;

import java.io.DataOutputStream;
import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Utilities;

import android.content.Context;
import android.telephony.TelephonyManager;


public class PhoneManager {

	private static String TAG = "PhoneManager";
	
	/**
	 * 挂断电话
	 * @param cx
	 */
	public static void endCall(Context cx) 
	{ 
		TelephonyManager telMag = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
		Class<TelephonyManager> c = TelephonyManager.class;
		Method mthEndCall = null;

		try 
		{
			mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
			mthEndCall.setAccessible(true);
			ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,(Object[]) null);
			iTel.endCall();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public static boolean endCallRoot()
	{

		Process process = null;
		DataOutputStream os = null;
		try {
			String cmd="input keyevent 6";
			process = Runtime.getRuntime().exec("su"); //切换到root帐号
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}

		Utilities.logCat(TAG,"End call succ",Constants.LOG_INFO);
		return true;

	}
}
